package com.example.onlinemusic.controller;

import com.example.onlinemusic.constant.Constant;
import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.Result;
import com.example.onlinemusic.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Autowired
    private LoveMusicMapper loveMusicMapper;

    @RequestMapping("deletelovemusic")
    public Result deleteLoveMusic(Integer musicId,HttpSession session){
        //验证登录
        User user = (User) session.getAttribute(Constant.LOGINUSER_SESSION_KEY);
        int userId = user.getId();
        //判断
        if(musicId!=null){
            int ret = loveMusicMapper.deleteLoveMusic(musicId,userId);
            if(ret==0){
                return new Result(-1,"删除失败",false);
            }
        }
        return new Result(200,"删除成功",true);
    }
    @RequestMapping("/likemusic")
    public Result likeMusic(@RequestParam String id,HttpServletRequest req) {
        int musicId = Integer.parseInt(id);
        System.out.println("likeMusic->musicId: " + musicId);

        //没有session不创建
        HttpSession httpSession = req.getSession(false);
        if (httpSession == null ||
                httpSession.getAttribute(Constant.LOGINUSER_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new Result(-1, "没有登录", false);
        }

        User user = (User) httpSession.getAttribute(Constant.LOGINUSER_SESSION_KEY);
        int userId = user.getId();
        System.out.println("likeMusic->userID: " + userId);
        //查询当前⽤⼾是否点赞过该⾳乐

        Music music = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId, musicId);
        if (music != null) {
            return new Result(-1, "该⽤⼾点赞过该⾳乐", false);
        } else {
            int ret = loveMusicMapper.insertLoveMusic(userId, musicId);
            if (ret!=0) {
                return new Result(0, "点赞⾳乐成功", true);
            } else {
                return new Result(-1, "点赞⾳乐失败", false);
            }
        }
    }

    @RequestMapping("/findlovemusic")
    public Result findLoveMusic(@RequestParam(required = false)String musicName,HttpSession session){
        //1.检查是否登录

        //2.拿到用户id
        User user = (User) session.getAttribute(Constant.LOGINUSER_SESSION_KEY);
        int id = user.getId();
        List<Music> ls = null;
        //判断是否进行全部查询
        if(musicName==null){
            //全部查询
            ls = loveMusicMapper.findLoveMusicByUserId(id);
        }else {
            ls = loveMusicMapper.findLoveMusicByUserIdAndMusicName(id,musicName);
        }
        return new Result(200,"查询成功",ls);
    }
}